<?php
!defined('P_W') && exit('Forbidden');
require_once MODE . '/base/frontcontroller.php';

class shopController extends FrontController {
	var $_layoutFile = 'shop';
	var $_seopage;
	var $_defaultSeo;
	var $_seoname;
	/**
	 * 店铺首页
	 */
	function run() {
		$scondSaleService = House::loadService('secondsaleservice');
		$indexCondition = array();
		$this->_viewer->userInfo['business'] == 'hire' && $this->_setTemplate('shop.default');
		$extend = array('uid'=>$this->_viewer->brokerModule->uid,'state'=>0);
		list($total, $this->_viewer->saleResult) = $scondSaleService->getDetailList($indexCondition,'','','',1,8,$extend);
		$this->_viewer->current['home'] = 'class="current"';
		$secondHireService = House::loadService('secondhireservice');
		list($hireTotal, $this->_viewer->hireResult) = $secondHireService->getDetailList($indexCondition,'','','',1,8,$extend);
	}
	/**
	 * 二手房
	 */
	function sale() {
		list($page) = GP(array('page'));
		$page = (int) $page < 1 ? 1 : (int) $page;
		$offset = 8;
		$scondSaleService = House::loadService('secondsaleservice');
		$indexCondition = array();
		$extend = array('uid'=>$this->_viewer->brokerModule->uid,'state'=>0);
		$saleUrl = $this->_viewer->baseUrl.'a=sale&';
		if ($this->_viewer->saleprice) {
			$indexCondition['pricetype'] = $this->_viewer->saleprice;
			$saleUrl .= 'saleprice=' . $this->_viewer->saleprice . '&';
		}
		if ($this->_viewer->saleroom) {
			$indexCondition['roomtype'] = $this->_viewer->saleroom;
			$saleUrl .= 'saleroom=' . $this->_viewer->saleroom . '&';
		}
		list($total, $this->_viewer->result) = $scondSaleService->getDetailList($indexCondition,'','','',$page,$offset,$extend);
		$this->_viewer->pages = numofpage($total,$page,ceil($total/$offset), $saleUrl);
		$this->_viewer->current['sale'] = 'class="current"';
	}
	/**
	 * 出租房页面
	 */
	function hire() {
		list($page) = GP(array('page'));
		$page = (int) $page < 1 ? 1 : (int) $page;
		$offset = 8;
		$scondHireService = House::LoadService('SecondHireService');
		$indexCondition = array();
		$extend = array('uid'=>$this->_viewer->brokerModule->uid,'state'=>0);
		$hireUrl = $this->_viewer->baseUrl.'a=hire&';
		if ($this->_viewer->hireprice) {
			$indexCondition['pricetype'] = $this->_viewer->hireprice;
			$hireUrl .= 'hireprice=' . $this->_viewer->hireprice . '&';
		}
		if ($this->_viewer->hireroom) {
			$indexCondition['roomtype'] = $this->_viewer->hireroom;
			$hireUrl .= 'hireroom=' . $this->_viewer->hireroom . '&';
		}
		list($total, $this->_viewer->result) = $scondHireService->getDetailList($indexCondition,'','','',$page,$offset,$extend);
		$this->_viewer->pages = numofpage($total,$page,ceil($total/$offset), $hireUrl);
		$this->_viewer->current['hire'] = 'class="current"';
	}
	/**
	 * 个人信息
	 */
	function info() {
		$this->_viewer->customfield = L::config('customfield', 'customfield');
		$days = max(1, ceil(($GLOBALS['timestamp'] - $this->_viewer->brokerInfo['regdate']) / 86400));
		$this->_viewer->brokerInfo['averagepost'] = round($this->_viewer->brokerInfo['postnum'] / $days, 2);
		$this->_viewer->brokerInfo['onlinetime_s'] = floor($this->_viewer->brokerInfo['onlinetime'] / 3600);
		($this->_viewer->brokerInfo['gender'] == 0 && $this->_viewer->brokerInfo['gender'] = '保密') || ($this->_viewer->brokerInfo['gender'] = $this->_viewer->brokerInfo['gender'] == 1 ? '男':'女');
		$ltitle = L::config('ltitle','level');
		$this->_viewer->brokerInfo['ltitle'] = $ltitle[$this->_viewer->brokerInfo['memberid']];
		$this->_viewer->brokerInfo['stitle'] = $this->_viewer->brokerInfo['groupid'] != -1 ? $ltitle[$this->_viewer->brokerInfo['groupid']] : '';
		$this->_viewer->current['info'] = 'class="current"';
	}
	
	function setSeoParams($seopage='index',$name = '',$broker){
		global $db_bbsname;
		$seopage = $seopage ? $seopage : 'index';
		$title = $keywords = $descript = $areaname = '';
		if($seopage == 'index'){
			$title = $broker->realname.'的店铺 - '.$db_bbsname;
			$keywords = "{$broker->realname},{$broker->newplate}二手房,{$broker->newplate}租房,{$db_bbsname}";
			$descript = "{$broker->realname}店铺,主营：{$broker->newplate}二手房,{$broker->newplate}租房";
		}
		$defaultSeo = array('title' => $title , 'metaDescription' => $descript, 'metaKeywords' => $keywords);
		return array($seopage,$defaultSeo,$name);
	}
	
	function _before() {
		global $winduid,$house_field;
		$this->_viewer->house_field = $house_field;
		list($uid) = GP(array('uid'));
		!$uid && $uid = $winduid;
		if (!$uid) Error::showError('普通会员没有个人店铺');
		$brokerHelper = House::LoadClass('brokerhelper', 'helper');
		$this->_viewer->brokerModule = $brokerHelper->getBrokerModule($uid,1,1);
		$this->_viewer->brokerInfo = $this->_viewer->brokerModule->brokerInfo;
		$this->_viewer->faceUrl = $this->_viewer->brokerInfo['faceurl'][0];
		if (!$this->_viewer->brokerModule->uid) Error::showError('该店铺不存在');
		list($this->_viewer->saleprice, $this->_viewer->saleroom,$this->_viewer->hireprice,$this->_viewer->hireroom) = GP(array('saleprice', 'saleroom','hireprice','hireroom'));
		$saleService = $brokerHelper->getHouseSaleService('SecondSaleService');
		$this->_viewer->price = $saleService->getFieldsByType('sellprice');//出售价格区间
		$this->_viewer->hirePrice = $saleService->getFieldsByType('rentprice');//租金价格区间
		$this->_viewer->room = $saleService->roomType;//居室
		$this->_viewer->baseUrl = BASEURL.'q=shop&uid='.$uid.'&';
		$this->_viewer->plate = $house_field['plate'];
		$seo = $this->setSeoParams('index','',$this->_viewer->brokerModule);
		list($this->_seopage,$this->_defaultSeo,$this->_seoname) = $seo;
		return true;
	}
	
	function _after(){
		houseSeoSettings($this->_seopage,$this->_defaultSeo,$this->_seoname);
	}
}